package com.itheima.core.test;

import java.util.Date;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.itheima.core.po.Customer;
import org.junit.Assert;
public class CustomerDBtest {
	private SqlSession sqlSession;
	@Test
	public void findCustomerByIdTest()throws Exception {
		String xmlPath = "classpath*:applicationContext.xml";
		ApplicationContext application = new ClassPathXmlApplicationContext(xmlPath);
		SqlSessionFactory sqlSessionFactory =  (SqlSessionFactory) application.getBean("sqlSessionFactory");
/*		SqlSessionFactory sqlSessionFactory = (SqlSessionFactory) applicationContext.getBean("sqlSessionFactory");
*/
		sqlSession = sqlSessionFactory.openSession();
		Customer customer = sqlSession.selectOne("com.itheima.core.dao.CustomerDao.getCustomerById",14);
		System.out.println(customer);
		sqlSession.close();

	}
	
	
	@Test
	public void addUploadFileTest() throws Exception{
		String xmlPath = "classpath:applicationContext.xml";
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
		// 从Spring容器获得内容
		SqlSessionFactory sqlSessionFactory = (SqlSessionFactory) applicationContext.getBean("sqlSessionFactory");
		sqlSession = sqlSessionFactory.openSession();
		// 4、SqlSession执行映射文件中定义的SQL，并返回映射结果
		
		Customer customer = new Customer();
		Date currentTime = new Date();
		customer.setCust_id(3);
		customer.setCust_name("李四");
		customer.setCust_user_id(3);
		customer.setCust_create_id(3);
		customer.setCust_source("7");
		customer.setCust_industry("2");
		customer.setCust_level("22");
		customer.setCust_linkman("李四");
		customer.setCust_phone("10086");
		customer.setCust_mobile("10086");
		customer.setCust_zipcode("10086");
		customer.setCust_address("广东省广东科技学院");
		customer.setCust_createtime(currentTime);
		int rows = sqlSession.insert("com.itheima.core.dao.CustomerDao.createCustomer",customer);
		Assert.assertEquals(1, rows); //正确插入的返回值为1
		// 5、关闭SqlSession
		sqlSession.close();		
	}
	
	
	@Test
	public void deleteUploadFileTest() throws Exception{
		String xmlPath = "classpath:applicationContext.xml";
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
		// 从Spring容器获得内容
		SqlSessionFactory sqlSessionFactory = (SqlSessionFactory) applicationContext.getBean("sqlSessionFactory");
		sqlSession = sqlSessionFactory.openSession();
		// 4、SqlSession执行映射文件中定义的SQL，并返回映射结果
		
		int rows = sqlSession.delete("com.itheima.core.dao.CustomerDao.deleteCustomer",3);
		Assert.assertEquals(1, rows); //正确插入的返回值为1
		rows = 0;
		Customer customer = new Customer();
		Date currentTime = new Date();
		customer.setCust_id(3);
		customer.setCust_name("李四");
		customer.setCust_user_id(3);
		customer.setCust_create_id(3);
		customer.setCust_source("7");
		customer.setCust_industry("2");
		customer.setCust_level("22");
		customer.setCust_linkman("李四");
		customer.setCust_phone("10086");
		customer.setCust_mobile("10086");
		customer.setCust_zipcode("10086");
		customer.setCust_address("广东省广东科技学院");
		customer.setCust_createtime(currentTime);
		
		rows = sqlSession.insert("com.itheima.dao.UploadFileDao.addUploadFile",customer);
		Assert.assertEquals(1, rows); //正确插入的返回值为1

		// 5、关闭SqlSession
		sqlSession.close();		
	}
	
	
}
